講完了概念,今天的鐵人文章內容,主要會透過實作的方式來告訴大家如何使用 Spring Data JPA 達到與資料庫中資料表的交互作業。
假設本次的實作目標是希望透過 API 取得資料庫中特定學生的基本資料還有考試成績,在達成這個目標前,我們需要先完成一些前置作業如下:
Step 1. 需要在 pom.xml 加入 spring data JPA 以及 資料庫的依賴項目
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.7.18</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.4.2.jre11</version>
</dependency>
引入資料庫依賴時,需根據開發時選定的資料庫來選擇對應的依賴項目。由於本次實作使用的是 MSSQL,因此引入的依賴項目為 MSSQL。
如果想要找尋相關的依賴項目,可以透過公開的Maven 尋找,並選擇合適的版本。
Step 2. 需要在 application.properties 內設定資料庫的連接方式
spring.datasource.url=jdbc:sqlserver://localhost:1433
spring.datasource.username=youraccount
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
# JPA 配置(可選)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect
Step 3. 在資料庫中建立對應的資料表
建立一個學生基本資訊的資料表 包含姓名 班級 學號
USE education;
GO
CREATE TABLE student_information (
si_id BIGINT PRIMARY KEY IDENTITY(1,1),
si_name NVARCHAR(50) NOT NULL, -- 學生姓名
si_serial_number NVARCHAR(20) NOT NULL, -- 學號
si_sex NVARCHAR(10) NOT NULL, -- 性別
si_class NVARCHAR(10) NOT NULL -- 班級
);
建立一個學生成績的資料表 包含 科目 哪個學期的考試
USE education;
GO
CREATE TABLE student_scores (
ss_id BIGINT PRIMARY KEY IDENTITY(1,1),
ss_si_id BIGINT NOT NULL, -- 關聯至 student_information 表的 si_id
ss_exam_id NVARCHAR(20) NOT NULL, -- 考試Id
ss_subject NVARCHAR(50) NOT NULL, -- 考試科目
ss_score INT NOT NULL -- 分數
);
Step 4. 建立實體類 (@Entity)
@Entity
@Table(name = "student_information", schema = "dbo", catalog = "education")
public class StudentInformation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "si_id", unique = true, nullable = false)
private long siId;
@Column(name = "si_name", nullable = false)
private String siName;
@Column(name = "si_sex", nullable = false)
private String siSex;
@Column(name = "si_serial_number", unique = true, nullable = false)
private String siSerialNumber;
@Column(name = "si_class", nullable = false)
private String siClass;
StudentInformation() {
}
public long getSiId() {
return siId;
}
public void setSiId(long siId) {
this.siId = siId;
}
public String getSiName(){
return siName;
}
public void setSiName(String siName){
this.siName = siName;
}
public String getSiSex(){
return siSex;
}
public void setSiSex(String siSex){
this.siSex = siSex;
}
public String getSiSerialNumber(){
return siSerialNumber;
}
public void setSiSerialNumber(String siSerialNumber){
this.siSerialNumber = siSerialNumber;
}
public String getSiClass(){
return siClass;
}
public void setSiClass(String siClass){
this.siClass = siClass;
}
}
@Entity
@Table(name = "student_scores", schema = "dbo", catalog = "education")
public class StudentScores {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ss_id", unique = true, nullable = false)
private long ssId;
@Column(name = "ss_si_id", nullable = false)
private long ssSiId;
@Column(name = "ss_exam_id", nullable = false)
private String ssExamId;
@Column(name = "ss_subject", nullable = false)
private String ssSubject;
@Column(name = "ss_score", nullable = false)
private String ssScore;
public StudentScores() {
}
public long getSsId() {
return ssId;
}
public void setSsId(long ssId) {
this.ssId = ssId;
}
public long getSsSiId() {
return ssSiId;
}
public void setSsSiId(long ssSiId) {
this.ssSiId = ssSiId;
}
public String getSsExamId() {
return ssExamId;
}
public void setSsExamId(String ssExamId) {
this.ssExamId = ssExamId;
}
public String getSsSubject() {
return ssSubject;
}
public void setSsSubject(String ssSubject) {
this.ssSubject = ssSubject;
}
public String getSsScore() {
return ssScore;
}
public void setSsScore(String ssScore) {
this.ssScore = ssScore;
}
}
Step 5. 建立 Repository
public interface StudentInformationRepository extends JpaRepository<StudentInformation, Long> {
}
public interface StudentScoresRepository extends JpaRepository<StudentScores, Long> {
}